﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DSAManager.Core;
using DSAManager.Models.Input;
using DSAManager.Models.Output;
using DSAManager.Utils;

namespace DSAManager
{
    public partial class FrmPhotoSize : Form
    {
        public FrmPhotoSize()
        {
            InitializeComponent();
        }

        private void btn_new_Click(object sender, EventArgs e)
        {
            resetInput();
        }

        void resetInput()
        {
            txt_name.Clear();
            num_heigth.Value = 0;
            num_width.Value = 0;
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            PhotoSizeSubmitInfo info=new PhotoSizeSubmitInfo();
            info.name = txt_name.Text;
            info.width = Convert.ToSingle(num_width.Value);
            info.heigth = Convert.ToSingle(num_heigth.Value);
            if(string.IsNullOrEmpty(info.name))
            {

                MsgBox.Alert("尺寸名称不能为空!");
                return;
            }
            var result = Api.SavePhotosize(info);
            if(result.ok)
            {
                queryPhotosize();
                MsgBox.Alert("保存成功!");
            }
            else
            {
                MsgBox.Alert("保存失败!");
            }
        }

        private void FrmPhotoSize_Load(object sender, EventArgs e)
        {
            queryPhotosize();
        }

        List<PhotoSizeInfo> sizeInfos=new List<PhotoSizeInfo>();
        void queryPhotosize()
        {
            
            BackgroundWorker bk=new BackgroundWorker();
            bk.DoWork += delegate
                             {
                                 sizeInfos.Clear();
                                 sizeInfos = Api.QueryPhotosize().data;
                             };
            bk.RunWorkerCompleted += delegate
                                         {
                                             list_size.Items.Clear();
                                             foreach (var info in sizeInfos)
                                             {
                                                 ListViewItem item = list_size.Items.Add(info.name);
                                                 item.SubItems.Add(info.width.ToString());
                                                 item.SubItems.Add(info.height.ToString());
                                                 item.Tag = info;
                                             }
                                             if(list_size.Items.Count>0)
                                             {
                                                 list_size.Items[0].Selected = true;
                                                 list_size.Items[0].EnsureVisible();
                                                 //list_size.Items[0].
                                             }
                                         };
            bk.RunWorkerAsync();

        }

        private void list_size_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(list_size.SelectedItems.Count<=0)return;
            var item = list_size.SelectedItems[0];

            var info = item.Tag as PhotoSizeInfo;
            txt_name.Text = info.name;
            num_width.Value =Convert.ToDecimal(info.width);
            num_heigth.Value = Convert.ToDecimal(info.height);

        }
    }
}
